package com.quan.xczs.rpc.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Condition;

import com.quan.base.common.base.service.support.BaseService;
import com.quan.base.common.base.web.support.BaseResult;
import com.quan.base.common.exception.BizException;
import com.quan.base.common.utils.date.DateUtils;
import com.quan.xczs.rpc.api.XczsConfigService;
import com.quan.xczs.rpc.api.XczsCouponService;
import com.quan.xczs.rpc.api.XczsUserCouponService;
import com.quan.xczs.rpc.api.XczsUserInvitedService;
import com.quan.xczs.rpc.api.XczsUserService;
import com.quan.xczs.rpc.dao.mapper.XczsUserMapper;
import com.quan.xczs.rpc.model.XczsConfig;
import com.quan.xczs.rpc.model.XczsCoupon;
import com.quan.xczs.rpc.model.XczsUser;
import com.quan.xczs.rpc.model.XczsUserCoupon;
import com.quan.xczs.rpc.model.XczsUserInvited;

@Service
public class XczsUserServiceImpl extends BaseService<XczsUser, Integer> implements XczsUserService {

	@Autowired
	private XczsUserMapper mapper;

	@Autowired
	private XczsUserInvitedService xczsUserInvitedService;

	@Autowired
	private XczsUserCouponService xczsUserCouponService;
	
	@Autowired
	private XczsConfigService xczsConfigService;

	@Autowired
	private XczsCouponService xczsCouponService;

	@Transactional
	@Override
	public int delete(Integer id) throws BizException {
		// 删除推荐人所关联的注册邀请人
		XczsUser selectById = super.selectById(id);
		this.xczsUserInvitedService.deleteByRefereeOpenid(selectById.getOpenid());

		//删除用户与优惠券关联
		this.xczsUserCouponService.deleteByParams(selectById.getMobile(), null, null);
		
		return super.delete(id);
	}

	@Transactional
	@Override
	public int deleteByIds(String ids) throws BizException {
		// 删除推荐人所关联的注册邀请人
		String[] split = ids.split(",");
		for (String id : split) {
			XczsUser selectById = super.selectById(Integer.parseInt(id));
			this.xczsUserInvitedService.deleteByRefereeOpenid(selectById.getOpenid());
			
			//删除用户与优惠券关联
			this.xczsUserCouponService.deleteByParams(selectById.getMobile(), null, null);
		}

		return super.deleteByIds(ids);
	}

	@Override
	public void deleteByOpenId(String openid) {
		Condition condition = new Condition(XczsUser.class);
		condition.createCriteria().andEqualTo("openid", openid);
		this.mapper.deleteByCondition(condition);
	}

	@Override
	public XczsUser getUserByMobile(String mobile) {
		Condition condition = new Condition(XczsUser.class);
		condition.createCriteria().andEqualTo("mobile", mobile);
		List<XczsUser> selectByCondition = this.mapper.selectByCondition(condition);
		if (null != selectByCondition && selectByCondition.size() == 1) {
			return selectByCondition.get(0);
		} else {
			return null;
		}
	}

	@Override
	public XczsUser getUserByOpenId(String openid) {
		Condition condition = new Condition(XczsUser.class);
		condition.createCriteria().andEqualTo("openid", openid);
		List<XczsUser> selectByCondition = this.mapper.selectByCondition(condition);
		if (null != selectByCondition && selectByCondition.size() == 1) {
			return selectByCondition.get(0);
		} else {
			return null;
		}
	}

	@Override
	public int registerSetValidCode(String openid, String validCode, int expire_minute) {
		XczsUser entity = new XczsUser();
		entity.setValidCode(validCode);
		entity.setValidCodeExpire(DateUtils.addMinute(expire_minute));

		Condition condition = new Condition(XczsUser.class);
		condition.createCriteria().andEqualTo("openid", openid);

		return this.mapper.updateByConditionSelective(entity, condition);
	}

	@Transactional
	@Override
	public BaseResult registerBinding(String openid, String realName, String mobile) {
		XczsUser entity = new XczsUser();
		entity.setRealName(realName);
		entity.setMobile(mobile);
		entity.setValidCode("");
		entity.setValidCodeExpire(0L);

		Condition condition = new Condition(XczsUser.class);
		condition.createCriteria().andEqualTo("openid", openid);
		this.mapper.updateByConditionSelective(entity, condition);

		//设置用户与优惠券关联
		return this.setUserCoupon(openid, mobile);
	}

	@Transactional
	@Override
	public BaseResult registerBinding(String openid, String realName, String mobile, XczsUserInvited invited) {
		XczsUser entity = new XczsUser();
		entity.setRealName(realName);
		entity.setMobile(mobile);
		entity.setValidCode("");
		entity.setValidCodeExpire(0L);

		Condition condition = new Condition(XczsUser.class);
		condition.createCriteria().andEqualTo("openid", openid);
		this.mapper.updateByConditionSelective(entity, condition);

		this.xczsUserInvitedService.insertSelective(invited);
		
		//设置用户与优惠券关联
		return this.setUserCoupon(openid, mobile);

	}

	/**
	 * 设置用户与优惠券关联
	 * @param openid
	 * @param mobile
	 */
	private BaseResult setUserCoupon(String openid, String mobile) {
		// 获取全局配置
		XczsConfig config = this.xczsConfigService.getConfig();
		// 判断是否关联优惠券
		if (config.getCouponEnabled() == 1 && config.getCouponId() != null) {
			// 优惠券ID
			Integer couponId = config.getCouponId();
			
			XczsCoupon coupon = this.xczsCouponService.selectById(couponId);
			if(null != coupon) {
				if(coupon.getProvideType() == 1){//限期发放
					if(coupon.getProvideCount() <= 0) {//优惠券已领完完
						return BaseResult.failure("优惠券已领取完");
					}
				}
					
				XczsUserCoupon userCoupon = new XczsUserCoupon();
				userCoupon.setXczsCouponId(couponId);
				userCoupon.setXczsUserMobile(mobile);
				userCoupon.setXczsUserOpenid(openid);
				this.xczsUserCouponService.insertSelective(userCoupon);
				
				//修改优惠券领取数量
				this.xczsCouponService.uploadActivateCount(couponId);
			}
		}
		return BaseResult.ok("ok");
	}

}
